﻿<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Responsavel extends Controller_Restrito_Template {

	public function action_index()
	{
		$view = new View('responsavel/index');
		$view->responsaveis = ORM::factory('responsavel')
			->order_by('nome')
			->find_all();
		
		$this->template->body = $view;
	}
	
	public function action_buscar()
	{
		$view = new View('responsavel/buscar');
		
		if(isset($_POST['buscar']))
		{
			$responsaveis = ORM::factory('responsavel')
				->where('nome', 'LIKE', '%'.$_POST['nome'].'%')
				->order_by('nome')
				->find_all();
			
			$view->responsaveis = $responsaveis;
		}
		
		if(!isset($view->responsaveis))
		{
			$view->responsaveis = ORM::factory('responsavel')
				->order_by('nome')
				->find_all();
		}
		
		$this->template->body = $view;
	}
	
	public function action_cadastrar()
	{
		$view = new View('responsavel/cadastrar');
		
		if(isset($_POST['cadastrar'])):
		
			$post = Validation::factory($_POST);
			$post->rule('nome', 'not_empty')
				//->rule('nome', 'regex', array(':value', '/^[a-zA-Z_ .]++$/iD'))
				//->rule('telefone', 'not_empty')
				->rule('telefone', 'min_length', array(':value', '3'))
				//->rule('email', 'not_empty')
				->rule('email', 'email')
				->rule('email', 'email_domain')
				//->rule('setor', 'not_empty')
				//->rule('setor', 'regex', array(':value', '/^[a-zA-Z_ .]++$/iD'))
				;
			
			if($post->check())
			{
				if(ORM::factory('responsavel')
					->where('nome', '=', $post['nome'])
					->and_where('setor', '=', $post['setor'])
					->find()->loaded())
				{
					Flash::message('success', 'Responsável já cadastrado!');
					$this->request->redirect('responsavel/cadastrar');
				}
				$responsavel = ORM::factory('responsavel');
				$responsavel->nome = $post['nome'];
				$responsavel->telefone = $post['telefone'];
				$responsavel->email = $post['email'];
				$responsavel->setor = $post['setor'];
				
				if($responsavel->save())
				{
					Flash::message('success', 'Responsável salvo com sucesso!');
				}
				else
					Flash::message('error', 'Falha ao salvar o Responsável!');
			}
			else {
				Flash::message('error', 'Confira os dados do Responsável!');
			}
		endif;
		
		$view->responsaveis = ORM::factory('responsavel')
			->order_by('nome')
			->find_all();
		
		$this->template->body = $view;
	}
	
	
	public function action_editar()
	{
		$view = new View('responsavel/editar');
		
		$responsavel = ORM::factory('responsavel', $this->request->param('id'));
		
		if($responsavel->loaded() && isset($_POST['alterar'])):
		
			$post = Validation::factory($_POST);
			$post->rule('nome', 'not_empty')
				//->rule('nome', 'regex', array(':value', '/^[a-zA-Z_ .]++$/iD'))
				//->rule('telefone', 'not_empty')
				->rule('telefone', 'min_length', array(':value', '3'))
				//->rule('email', 'not_empty')
				->rule('email', 'email')
				->rule('email', 'email_domain')
				//->rule('setor', 'not_empty')
				//->rule('setor', 'regex', array(':value', '/^[a-zA-Z_ .]++$/iD'))
				;
			
			if($post->check() && $this->request->param('id'))
			{
				
				$responsavel->nome = $post['nome'];
				$responsavel->telefone = $post['telefone'];
				$responsavel->email = $post['email'];
				$responsavel->setor = $post['setor'];
				
				if($responsavel->save())
				{
					Flash::message('success', 'Responsável alterado com sucesso!');
					$this->request->redirect('responsavel/index');
				}
				
				Flash::message('error', 'Falha ao salvar o Responsável!');
			}
			
		endif;
		
		$view->responsavel = $responsavel;
		$view->responsaveis = ORM::factory('responsavel')->order_by('nome')->find_all();
		
		$this->template->body = $view;
	}
	
	public function action_ver()
	{
		$view = new View('responsavel/ver');
		
		$view->responsavel = ORM::factory('responsavel', $this->request->param('id'));
		
		$this->template->body = $view;
	}
	
	public function action_apagar()
	{
		$responsavel = ORM::factory('responsavel', $this->request->param('id'));
		if($responsavel->loaded())
		{
			if($responsavel->delete())
			{
				Flash::message('success', 'Responsável apagado com sucesso!');
			}
			else {
				Flash::message('error', 'Falha ao apagar o Responsável!');
			}
		}
		else {
			Flash::message('error', 'O Responsável é inválido!');
		}
	
		$this->request->redirect('responsavel/index');
	}

} // End Responsavel
